package com.example.demo.model.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

/**
 * 统一API响应结果封装
 * @param <T> 数据类型
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@Schema(description = "统一响应结果")
public class ResponseResult<T> {

    @Schema(description = "状态码", example = "200")
    private int code;

    @Schema(description = "消息", example = "操作成功")
    private String message;

    @Schema(description = "返回数据")
    private T data;

    /**
     * 成功响应（带数据）
     */
    public static <T> ResponseResult<T> success(T data) {
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(200);
        result.setMessage("操作成功");
        result.setData(data);
        return result;
    }

    /**
     * 成功响应（不带数据）
     */
    public static <T> ResponseResult<T> success() {
        return success(null);
    }

    /**
     * 失败响应
     */
    public static <T> ResponseResult<T> error(int code, String message) {
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    /**
     * 失败响应（带数据）
     */
    public static <T> ResponseResult<T> error(int code, String message, T data) {
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(code);
        result.setMessage(message);
        result.setData(data);
        return result;
    }

    // 快速构建常见错误响应
    public static <T> ResponseResult<T> badRequest(String message) {
        return error(400, message);
    }

    public static <T> ResponseResult<T> unauthorized(String message) {
        return error(401, message);
    }

    public static <T> ResponseResult<T> forbidden(String message) {
        return error(403, message);
    }

    public static <T> ResponseResult<T> notFound(String message) {
        return error(404, message);
    }

    public static <T> ResponseResult<T> serverError(String message) {
        return error(500, message);
    }
}